package com.cat.binaryTree;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 曲大人的喵
 * @description Solution42
 * @create 2025/9/15 10:28
 * @since JDK17
 */

public class Solution42 {
    TreeNode s;
    int start;
    HashMap<TreeNode, TreeNode> map = new HashMap<>();
    void f(TreeNode root, TreeNode from) {
        if (root == null) {
            return ;
        }
        if (root.val == start) {
            s = root;
        }
        map.put(from, root);
        f(root.left, root);
        f(root.right, root);
    }

    int dfs(TreeNode root, TreeNode from) {
        if (root == null) {
            return -1;
        }
        int res = -1;
        if (root.left != from) {
            res = Math.max(res, dfs(root.left, root));
        }
        if (root.right != from) {
            res = Math.max(res, dfs(root.right, root));
        }
        if (map.get(root) != from) {
            res = Math.max(res, dfs(map.get(root), root));
        }
        return res + 1;
    }
    public int amountOfTime(TreeNode root, int start) {
        this.start = start;
        f(root, null);
        return dfs(root, null);
    }
}
